import { resetUserPassword } from '@/services/geographic';
import { roleAuthorization, updateRole, delRole, getRoleByRoleId, getRolesByCreateUserPage, getMenuList, getAuthTree, updateAuthorization, addRole, getMenus, doAllSysMenu, doUserMenu, fetchAuthRole, fetchUpdateAuthRole } from '@/services/user';
export default {
  namespace: 'authManageOld',
  state: {
    total: 0,
    items: [],
    loading: true
  },
  effects: {
    *getRoleLists({ payload, callback }, { call, put }) {
      const response = yield call(getRolesByCreateUserPage, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addRoles({ payload, callback }, { call, put }) {
      const response = yield call(addRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addAuthForRole({ payload, callback }, { call, put }) {
      const response = yield call(roleAuthorization, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *editAuthForRole({ payload, callback }, { call, put }) {
      const response = yield call(updateAuthorization, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *updateRoles({ payload, callback }, { call, put }) {
      const response = yield call(updateRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *resetUser({ payload, callback }, { call, put }) {
      const response = yield call(resetUserPassword, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *delRoles({ payload, callback }, { call, put }) {
      const response = yield call(delRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getRoleByRoleIds({ payload, callback }, { call, put }) {
      const response = yield call(getRoleByRoleId, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getMenuListByRoleId({ payload, callback }, { call, put }) {
      const response = yield call(getMenuList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getmenusByUserIds({ payload, callback }, { call, put }) {
      const response = yield call(getMenus, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getAuthTreeBy({ payload, callback }, { call, put }) {
      const response = yield call(getAuthTree, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getAllSysMenu({ payload, callback }, { call, put }) {
      const response = yield call(doAllSysMenu, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getuserMenu({ payload, callback }, { call, put }) {
      const response = yield call(doUserMenu, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addAuthRole({ payload, callback }, { call, put }) {
      const response = yield call(fetchAuthRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *updateAuthRole({ payload, callback }, { call, put }) {
      const response = yield call(fetchUpdateAuthRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
